import java.io.*;
import java.util.Date;

public class Run {
	static String[] xhtmlDocument = { "Document.html" };

	public static void main(String[] args) {

		FileOutputStream writer;
		boolean jDOMFailure = false;
		try {
			System.out.println("Building Document with 2000 input elements...");
			makeDoc(1000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (RuntimeException e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 10000 input elements...");
			makeDoc(5000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 15000 input elements...");
			makeDoc(7500);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 20000 input elements...");
			makeDoc(10000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out.println("Building Document with 2000 input elements...");
			makeDoc(1000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}

			System.out
					.println("Building Document with 40000 input elements...");
			makeDoc(20000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 100000 input elements...");
			makeDoc(50000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 200000 input elements...");
			makeDoc(100000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 300000 input elements...");
			makeDoc(150000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 500000 input elements...");
			makeDoc(250000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}
			System.out
					.println("Building Document with 1000000 input elements...");
			makeDoc(500000);
			System.out.println("Running SAX");
			SAXSolution.main(xhtmlDocument);
			if (!jDOMFailure) {
				try {
					System.out.println("Running JDOM");
					JDOMSolution.main(xhtmlDocument);
				} catch (OutOfMemoryError e) {
					jDOMFailure = true;

					e.printStackTrace();
				}
			}

		} catch (IOException e) {

		}
	}

	public static void makeDoc(int amount) throws IOException {
		Date start = new Date();
		String inputElement = "<input name=\"question\" type=\"text\" size=\"40\"/>?<br/>"
				+ "<input type=\"submit\" name=\"submit\" value=\"Register my question\"/>";
		FileOutputStream writer = new FileOutputStream("Document.html");

		new PrintStream(writer)
				.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
						+ "<html xmlns=\"http://www.w3.org/1999/xhtml\">"
						+ "<head><title>QuickPoll</title></head><body>"
						+ "<h1>QuickPoll</h1>"
						+ "<form method=\"post\" action=\"setup\">"
						+ "What is your question?<br/>");

		for (int i = 0; i < amount; i++) {
			new PrintStream(writer).println(inputElement);
		}
		new PrintStream(writer).println("</form></body></html>");
		writer.close();

		Date finish = new Date();
		long finishTime = finish.getTime() - start.getTime();
		System.out.println("Building of string complete in " + finishTime
				+ "ms");
	}
}
